package com.credit.core.util;

import java.io.Serializable;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

/**
 * 
 * 项目名称：core 类名称：ActivemqUtil 类描述： activeMQ工具 创建人：huan 创建时间：2016年3月16日 下午2:03:25
 * 修改人：huan 修改时间：2016年3月16日 下午2:03:25 修改备注：
 * 
 * @version
 * 
 */
public class ActivemqUtil {

    private static Connection connection;

    public static final String QUEUEMSG = "sitemsg-queue";

    public static final String QUUEINDEXARTICLE = "indexArticle-queue";

    public static final void setConnection(Connection connection) {
        ActivemqUtil.connection = connection;
    }

    public static void sendMessageToQueue(String queue, String content) {
        try {
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queue);
            // 得到消息生成者【发送者】
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            producer.send(session.createTextMessage(content));
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void sendMessageToQueue(String queue, Serializable obj) {
        try {
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queue);
            // 得到消息生成者【发送者】
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            ObjectMessage om = session.createObjectMessage();
            om.setObject(obj);
            producer.send(om);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void sendMessageToQueue(String queue, Map<String, Object> messageMap) {
        try {
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queue);
            // 得到消息生成者【发送者】
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            MapMessage mm = session.createMapMessage();
            for (Map.Entry<String, Object> entry : messageMap.entrySet()) {
                mm.setObject(entry.getKey(), entry.getValue());
            }
            producer.send(mm);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void sendMessageToTopic(String topic, String content) {
        try {
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic(topic);
            // 得到消息生成者【发送者】
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            producer.send(session.createTextMessage(content));
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void sendMessageToTopic(String topic, Map<String, Object> messageMap) {
        try {
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic(topic);
            // 得到消息生成者【发送者】
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            MapMessage mm = session.createMapMessage();
            for (Map.Entry<String, Object> entry : messageMap.entrySet()) {
                mm.setObject(entry.getKey(), entry.getValue());
            }
            producer.send(mm);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
